package com.app.service.Impl;

import com.app.mapper.EntrepreneurshipMapper;
import com.app.mapper.UserMapper;
import com.app.mapper.VillageFairMapper;
import com.app.model.EntrepreneurshipModel;
import com.app.model.UserModel;
import com.app.model.VillageFairModel;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.WechatUtil;
import com.app.vo.EntrepreneurshipVo;
import com.app.vo.FairVo;
import com.app.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EntrepreneurshipMapper entrepreneurshipMapper;
    @Autowired
    private VillageFairMapper villageFairMapper;

    @Override
    public void update(UserModel model) {
        userMapper.updateById(model);
    }

    @Override
    public void insert(UserModel model) {
        userMapper.insert(model);
    }

    @Override
    public UserModel selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public Object getPublishById(Integer id, Integer type, Integer status) {
        if (type == 1) {
            EntrepreneurshipVo vo = new EntrepreneurshipVo();
            QueryWrapper<EntrepreneurshipModel> queryWrapper = new QueryWrapper<>();
            if (status >= 0) {
                queryWrapper.eq("status", status);
            }
            queryWrapper.eq("user_id", id);
            List<EntrepreneurshipModel> entrepreneurshipModels = entrepreneurshipMapper.selectList(queryWrapper);
            if (!entrepreneurshipModels.isEmpty()) {
                for (int i = 0; i < entrepreneurshipModels.size(); i++) {
                    if (entrepreneurshipModels.get(i).getStatus() == 0) {
                        entrepreneurshipModels.get(i).setStatusText("待审核");
                    }
                    if (entrepreneurshipModels.get(i).getStatus() == 1) {
                        entrepreneurshipModels.get(i).setStatusText("通过");
                    }
                    if (entrepreneurshipModels.get(i).getStatus() == 2) {
                        entrepreneurshipModels.get(i).setStatusText("未通过");
                    }
                }
                for (int i = 0; i < entrepreneurshipModels.size(); i++) {
                    if (entrepreneurshipModels.get(i).getSwiperImage() != null && !entrepreneurshipModels.get(i).getSwiperImage().equals("")) {
                        String[] split = entrepreneurshipModels.get(i).getSwiperImage().split(",");
                        entrepreneurshipModels.get(i).setSwiperImages(Arrays.asList(split));
                    }
                }
            }
            Integer count = entrepreneurshipMapper.selectCount(queryWrapper);
            vo.setList(entrepreneurshipModels);
            vo.setCount(count);
            return vo;
        } else {
            FairVo vo = new FairVo();
            QueryWrapper<VillageFairModel> queryWrapper = new QueryWrapper<>();
            if (status >= 0) {
                queryWrapper.eq("status", status);
            }
            queryWrapper.eq("user_id", id);
            List<VillageFairModel> villageFairModels = villageFairMapper.selectList(queryWrapper);
            if (!villageFairModels.isEmpty()) {
                for (int i = 0; i < villageFairModels.size(); i++) {
                    if (villageFairModels.get(i).getStatus() == 0) {
                        villageFairModels.get(i).setStatusText("待审核");
                    }
                    if (villageFairModels.get(i).getStatus() == 1) {
                        villageFairModels.get(i).setStatusText("通过");
                    }
                    if (villageFairModels.get(i).getStatus() == 2) {
                        villageFairModels.get(i).setStatusText("未通过");
                    }
                }
                for (int i = 0; i < villageFairModels.size(); i++) {
                    if (villageFairModels.get(i).getSwiperImage() != null && !villageFairModels.get(i).getSwiperImage().equals("")) {
                        String[] split = villageFairModels.get(i).getSwiperImage().split(",");
                        villageFairModels.get(i).setSwiperImages(Arrays.asList(split));
                    }
                }
            }
            Integer count = villageFairMapper.selectCount(queryWrapper);
            vo.setList(villageFairModels);
            vo.setCount(count);
            return vo;
        }
    }

    @Override
    public UserVo getList(Integer page, Integer limit, String keyword) {
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.like("name", keyword).or().like("phone", keyword);
        queryWrapper.orderByDesc("id");
        Page<UserModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<UserModel> userModelPage = userMapper.selectPage(pages, queryWrapper);
        UserVo vo = new UserVo();
        vo.setList(userModelPage.getRecords());
        vo.setCount((int) userModelPage.getTotal());
        return vo;
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        UserModel model = new UserModel();
        model.setId(id);
        model.setStatus(status);
        userMapper.updateById(model);
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public UserModel getOpenid(String code, HttpServletRequest request) throws LoginException {
        //获取openid
        String openid = WechatUtil.getOpenid(code);
        if (openid == null) {
            throw new LoginException("openid为空");
        }
        //根据openid查询用户信息
        UserModel user = userMapper.getByOpenid(openid);
        if (user == null) {
            //当前是新用户，自动注册
            user = new UserModel();
            user.setName("");
            user.setAvatar("");
            user.setPhone("");
            user.setStatus(1);
            user.setOpenId(openid);
            user.setCreateTime(CommonHelp.getNowTimestamp());
            userMapper.insert(user);
        }
        return user;
    }

    @Override
    public String getPhone(String code, HttpServletRequest request) {
        String mobile = WechatUtil.getMobile(code);
        return mobile;
    }

    @Override
    public UserModel getUserByOpenid(String mobile) {
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", mobile);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public UserModel selectUserById(Integer id) {
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "phone", "avatar", "status", "create_time", "update_time");
        queryWrapper.eq("id", id);
        return userMapper.selectOne(queryWrapper);
    }
}
